function [Gx, Gy] = imgrad(I)
%IMGRAD Computes the image gradient
%
%   [Gx, Gy] = IMGRAD(I);
%
%       Evaluates the gradient along x and y directions for an image I.
%
%       Here, I should be a real-valued matrix (double or single type)
%
%       Gx and Gy are of the same size and same type as I.
%
%   Remarks
%   -------
%       - In current implementation, sobel operator will be used to 
%         calculate the gradient map (with appropriate boundary
%         processing).
%

% Created by Dahua Lin, on April 4, 2012
%

%% verify input arguments

if ~(isfloat(I) && isreal(I) && ismatrix(I) && ~issparse(I))
    error('imgrad:invalidarg', 'I should be a non-sparse real matrix.');
end

%% main

Hx = [-1 0 1; -2 0 2; -1 0 1] * (1/8);
Hy = Hx.';

Gx = imfilter(I, Hx, 'replicate', 'same');
Gy = imfilter(I, Hy, 'replicate', 'same');

Gx(:,1)   = 2 * Gx(:,1);
Gx(:,end) = 2 * Gx(:,end);
Gy(1,:)   = 2 * Gy(1,:);
Gy(end,:) = 2 * Gy(end,:);

